const project = require("../models/project");
const formidable = require("formidable");

var express = require("express");
var router = express.Router();

router.post('/', (req, res) => {
    var form = formidable.IncomingForm();
    form.parse(req, (err, fields) => {
        if (err) {
            res.json({ 'state': false, 'msg': err });
        }else {
            project.create({
                project_name: fields.name,
                project_type: fields.type,
                difficult: fields.difficult,
                processes: fields.processes,
                ctime: new Date().getTime(),
                mtime: new Date().getTime()
            }, (error) => {
                if (error!=null) {
                    res.json({ 'state': false, 'msg': error });
                }else {
                    res.json({'state': true, 'msg': 'add project success'});
                }
            })
        }
    });
});

router.get('/get_all_projects', (req, res) => {
    var form = formidable.IncomingForm();
    form.parse(req, (err, fields) => {
        if (err) {
            res.json({ 'state': false, 'msg': err });
        }else {
            project.find({}, (error, doc) => {
                if (error) res.json({ 'state': false, 'msg': error });
                else {
                    res.json({
                        'state': true, 
                        'msg': 'find projects success',
                        'data': doc
                    })
                }
            })
        }
    });
});

router.get('/:id', (req, res) => {
    project.find({_id: req.params.id}, (error, doc) => {
        if (error) res.json({ 'state': false, 'msg': error });
        else {
            res.json({
                'state': true, 
                'msg': 'find project by id success',
                'data': doc
            })
        }
    })
});

router.put('/:id', (req, res) => {
    var form = formidable.IncomingForm();
    form.parse(req, (err, fields) => {
        if (err) {
            res.json({ 'state': false, 'msg': err });
        }else {
            project.findByIdAndUpdate(req.params.id, {
                project_name: fields.name,
                project_type: fields.type,
                difficult: fields.difficult,
                processes: fields.processes,
                mtime: new Date().getTime()
            }, {new: false}, (error,doc) => {
                if (error) res.json({ 'state': false, 'msg': error });
                else {
                    res.json({
                        'state': true, 
                        'msg': 'update project success',
                        'data': doc
                    })
                }
            })
        }
    });
});

router.delete('/:id', (req, res) => {
    project.remove({_id: req.params.id}, (error, doc) => {
        if (error) res.json({ 'state': false, 'msg': error });
        else {
            res.json({
                'state': true, 
                'msg': 'delete project success',
                'data': doc
            })
        }
    })
});


module.exports = router;
